#This file creates a table comparing descriptive data of voters in 2013 from municipalities in all municipalities, 
#only municipalities included in 2009, only included in 2014, only included in 2015, and included in all four elections.

# Load data
# Note that data has been aggregated by two days to avoid a small cell and abide by requirement to maintain voters' anonymosity 
# The data is aggregated so that parents on Election Day and the following day are collapsed.
# Consequently, this data is not appropriate for comparing the effect in the cutoff.

## commented out for replication files 

#  install.packages("xtable")
#  install.packages("dplyr")

library(xtable)
library(dplyr)

load("table_rep.rdata")

# descriptive table -------------------------------------------------------

table <- data_frame("Parents Voted  (%)"           = rep(NA, 5),
                    "Parents Native  (%)"          = rep(NA, 5),
                    "Parents' Age  (%)"            = rep(NA, 5),
                    "Parents' Income (1000 euro)"  = rep(NA, 5),
                    "Parents Female (%)"           = rep(NA, 5),
                    "Parents Degree (%)"           = rep(NA, 5),
                    "Child Female (%)"             = rep(NA, 5),
                    "Child Oldest (%)"             = rep(NA, 5),
                    "N"                            = rep(NA, 5),
                    "No. of Municipals"            = rep(NA, 5)  )

#Take weighted means
for (i in 1:5){
  for (j in 1:8){
    x <- unlist(data_reprentative[[i]][,(j+1)])
    table[i, j] <- weighted.mean(x = unlist(data_reprentative[[i]][,(j+1)])*100, 
                                 w = unlist(data_reprentative[[(i+5)]][,2]) )
  }
  table[i, 9] <- sum(data_reprentative[[(i+5)]][,2]) 
}

# inpuit number of municipalities included each year
table[, 10] <- c(98, 44, 61, 72, 32)


rownames(table) <- c("2013 (All)",
                     "2009",
                     "2014",
                     "2015",
                     "2009-2015")
#Rescale age and income
table[,3] <- table[,3]/36525 + 18
table[,4] <- table[,4]/746.25
                   
xtable(t(table), digits = cbind(1,
                                rbind(rep(2,nrow(table)),
                                      rep(2,nrow(table)),
                                      rep(2,nrow(table)),
                                      rep(0,nrow(table)),
                                      rep(2,nrow(table)),
                                      rep(2,nrow(table)),
                                      rep(2,nrow(table)),
                                      rep(2,nrow(table)),
                                      rep(0,nrow(table)),
                                      rep(0,nrow(table)))))
                                 

# 
